<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/2/22
 * Time: 下午4:06
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class GroupController extends AdminBaseController
{
    /**
     * 角色表
     */
    public function index()
    {
        $curr = I('get.p/d',0);
        $totalNum = 1;
        $group = M('group');
        $group_list = $group->page($curr.','.$totalNum)->select();
        $this->assign('group_list',$group_list);
        $count = $group->count();
        $Page  = new \Think\Page($count,$totalNum);
        $show  = $Page->show();
        $this->assign('page',$show);
        $this->assign('title','FD商城角色表');
        $this->display();
    }

    /**
     * 添加角色
     */
    public function add()
    {
        if(IS_GET){
            $this->assign('title','FD商城角色添加');
            $this->display();
            exit;
        }
        if(IS_AJAX){
            //获取角色中文名称
            $title = I('post.title','');
            if(empty($title)){
                $this->ajaxReturn(['state'=>1,'msg'=>'角色中文名称不能为空！']);
            }
            if(M('group')->where(array('title'=>$title))->getField('id')){
                $this->ajaxReturn(['state'=>1,'msg'=>'此角色已经存在！']);
            }
            if(M('group')->add(['title'=>$title,'rules'=>'','add_time'=>time()])){
                $this->ajaxReturn(['state'=>0,'msg'=>'添加角色成功！']);
            }
            $this->ajaxReturn(['state'=>1,'msg'=>'添加角色失败！']);
        }
    }

    /**
     * 修改角色
     */
    public function edit()
    {
        if(IS_GET){
            $id = I('get.id',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            $group_info = M('group')->field('id,title')->where(['id'=>$id])->find();
            $this->assign('group_info',$group_info);
            $this->assign('title','FD商城角色修改');
            $this->display();
        }
        if(IS_POST){
            $id = I('post.id',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            //获取角色中文名称
            $title = I('post.title','');
            if(empty($title)){
                $this->ajaxReturn(['state'=>1,'msg'=>'角色中文名称不能为空！']);
            }
            if(M('group')->where(['id'=>$id])->setField(['title'=>$title,'up_time'=>time()]) === false){
                $this->ajaxReturn(['state'=>1,'msg'=>'修改失败！']);
            }
            $this->ajaxReturn(['state'=>0,'msg'=>'修改成功！']);
        }
    }

    /**
     * 删除角色
     */
    public function del(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            if(M('admin_group')->where(['group_id' => $id])){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能删除有对应管理员的角色！']);
            }
            if(M('group')->where(['id'=>$id])->getField('id') == false){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能删除不存在的角色！']);
            }
            if(M('group')->delete($id) == false){
                $this->ajaxReturn(['state'=>1,'msg'=>'删除失败！']);
            }
            $this->ajaxReturn(['state'=>0,'msg'=>'删除成功！']);
        }else{
            $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
        }
    }

    /**
     * 修改角色状态
     */
    public function changeState(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            $group_status = M('group')->where(['id' => $id])->getField('status');
            if($group_status === false){
                $this->ajaxReturn(['state'=>1,'msg'=>'修改状态失败！']);
            }
            if($group_status){
                M('group')->where(['id' => $id])->setField('status',0);
                $this->ajaxReturn(['state'=>0,'msg'=>'已禁用！','data' => 1]);
            }else{
                M('group')->where(['id' => $id])->setField('status',1);
                $this->ajaxReturn(['state'=>0,'msg'=>'已启用！','data' => 0]);
            }
        }else{
            $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
        }
    }
}